package com.ketai.web;

import com.alibaba.fastjson.JSON;
import com.ketai.common.Result;
import com.ketai.common.StatusCode;
import com.ketai.pojo.CUser;
import com.ketai.query.ActivitesQuery;
import com.ketai.service.CUserService;
import com.ketai.service.impl.CUserServiceImpl;
import com.ketai.util.Pages;
import com.ketai.util.SendUtil;
import com.ketai.vo.ActivityVo;
import com.ketai.vo.CUserVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-08 9:29
 **/
@WebServlet("/cuserServlet")
public class CUserServlet extends HttpServlet {
    CUserService cUserService = new CUserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            Integer curr = Integer.parseInt(req.getParameter("page"));
            Integer limit = Integer.parseInt(req.getParameter("limit"));
            String start_time = req.getParameter("startTime");
            String end_time = req.getParameter("endTime");
            String title = req.getParameter("title");
            String type = req.getParameter("type");
            String checked = req.getParameter("checked");

            Pages<CUser> ActivitiePages = cUserService.getAllCUser(curr, limit);
            //调用工具类 返回数据给前端
            //SendUtil.SendPageJson(resp, ActivitiePages);
            //List<CUser> users = cUserService.getAllCUser(curr, limit);
            Map<String, Object> tableData = new HashMap<String, Object>();
            //这是layui要求返回的json数据格式
            tableData.put("code", 2000);
            tableData.put("msg", "");
            //将全部数据的条数作为count传给前台（一共多少条）
            tableData.put("count", ActivitiePages.getDataTotal());

            //将分页后的数据返回（每页要显示的数据）
            tableData.put("data", ActivitiePages.getDataItems());
            resp.getWriter().write(JSON.toJSONString(tableData));
        }
    }
}
